﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TheoriesBraun.BLL;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace TheoriesBraun.DAL
{
    class UserDAL
    {
        public UserDAL() { }

        public Boolean Login(UserBLL user)
        {
            string query = "SELECT Account, [Password] FROM [User] WHERE Account='" 
                                    + user.Account + "' AND [Password]='" 
                                    + user.Password + "' AND [Status]='1'";
            SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.HasRows)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public DataTable getAllUser()
        {
            DataTable dt = null;
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT * FROM [User]", DBConnect.getConnection());
                SqlDataReader reader = cmd.ExecuteReader();
                dt = new DataTable();
                dt.Load(reader);
                reader.Close();
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void addUser(UserBLL user)
        {
            try
            {
                string query = "INSERT INTO [User] VALUES ('" + user.Account + 
                    "', N'" + user.FullName + 
                    "', '" + user.Password + 
                    "', '" + user.Email + 
                    "', '" + user.Phone + 
                    "', N'" + user.Address + 
                    "', '" + user.Status + "')";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void editUser(UserBLL user)
        {
            try
            {
                string query = "UPDATE [User] SET Account = '" + user.Account +
                    "', FullName = N'" + user.FullName + 
                    "', Password = '" + user.Password + 
                    "', Email = '" + user.Email + 
                    "', PhoneNumber = '" + user.Phone + 
                    "', Address = N'" + user.Address + 
                    "', Status = '" + user.Status +
                    "' WHERE Account = '" + user.Account + "'";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void delUser(UserBLL user)
        {
            try
            {
                string query = "DELETE FROM [User] WHERE Account ='" + user.Account + "'";
                SqlCommand cmd = new SqlCommand(query, DBConnect.getConnection());
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
